﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SaleOnlineSystem.Admin.Controls
{
    public partial class AddProduct : System.Web.UI.UserControl
    {
        public string activesize;
        public string activediscount;
        public LIB.Validate saveimage;
        public string id = "";
        public int color_id;
        public int size_id;
        public string filePathImageAvatar;
        public string filePathBigImage;
        public string filePathSmallIamge;
        public string activelinkbaohanh;
        public string activespch;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            LIB.Validate saveimage = new LIB.Validate();
            if (!Page.IsPostBack)
            {
                // Them du lieu vao cat 1
                DataTable dt = DAL.DCollector.DBCollector.Instance.StoredProcedureSelectWhereByID("tbl_Category", "*", "Category_ID2", 0, "selectwhereid");
                ListItem lt = new ListItem("Chon danh muc", "-1");
                Cate1.Items.Add(lt);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    lt = new ListItem(dt.Rows[i]["Category_Name"].ToString(), dt.Rows[i]["Category_ID1"].ToString());
                    Cate1.Items.Add(lt);
                }
                Cate1.SelectedIndex = 0;

                DataTable dt2 = DAL.DCollector.DBCollector.Instance.SelectSQL("Select * from tbl_Color");
                rpt_Color.DataSource = dt2;
                rpt_Color.DataBind();                         
            }
        }

        protected void Cate1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                Cate2.Items.Clear();
                Cate3.Items.Clear();
                ddlVendor.Items.Clear();
                int index = Cate1.SelectedIndex;
                if (index > 0)
                {
                    int id = Convert.ToInt32(Cate1.SelectedValue);
                    // Load du lieu
                    DataTable dt = DAL.DCollector.DBCollector.Instance.StoredProcedureSelectWhereByID("tbl_Category", "*", "Category_ID2", id, "selectwhereid");
                    ListItem lt = new ListItem("Chon danh muc", "-1");
                    Cate2.Items.Add(lt);
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        lt = new ListItem(dt.Rows[i]["Category_Name"].ToString(), dt.Rows[i]["Category_ID1"].ToString());
                        Cate2.Items.Add(lt);
                    }
                    // Chen vao nha san xuat
                    dt = DAL.DCollector.DBCollector.Instance.StoredProcedureSelectWhereByID("tbl_Vendor", "*", "Category_ID1", id, "selectwhereid");
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        lt = new ListItem(dt.Rows[i]["Vendor_Name"].ToString(), dt.Rows[i]["Vendor_ID"].ToString());
                        ddlVendor.Items.Add(lt);
                    }
                }
            }
            catch (Exception)
            {
                Response.Redirect("/error.htm");
            }
        }

        protected void Cate2_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Chon danh muc cap 1 load danh muc cap 2      
            try
            {
                Cate3.Items.Clear();
                int index = Cate2.SelectedIndex;

                int id = Convert.ToInt32(Cate2.SelectedValue);
                // Load du lieu
                DataTable dt = DAL.DCollector.DBCollector.Instance.StoredProcedureSelectWhereByID("tbl_Category", "*", "Category_ID2", id, "selectwhereid");
                ListItem lt = new ListItem("Chon danh muc", "-1");
                Cate3.Items.Add(lt);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    lt = new ListItem(dt.Rows[i]["Category_Name"].ToString(), dt.Rows[i]["Category_ID1"].ToString());
                    Cate3.Items.Add(lt);
                }
            }
            catch (Exception)
            {
                Response.Redirect("/error.htm");
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string cate1_name = Cate1.SelectedItem.ToString();

            int c1 = Cate1.Items.Count;
            int c2 = Cate2.Items.Count;
            int c3 = Cate3.Items.Count;

            int cate1_index = Cate1.SelectedIndex;

            int cate2_index = Cate2.SelectedIndex;
            int cate3_index = Cate3.SelectedIndex;

            if (cate1_index > 0)
            {
                if ((c1 > 0 && c2 <= 1) || (c1 > 0 && c2 > 0 && c3 <= 1 && cate2_index > 0) || (c1 > 0 && c2 > 0 && c3 > 0 && cate3_index > 0))
                {
                    int cate1_id = Convert.ToInt32(Cate1.SelectedValue.ToString());

                    int cate2_id = 0;
                    int cate3_id = 0;

                    int vendor_id = Convert.ToInt32(ddlVendor.SelectedValue.ToString());
                    if (cate2_index < 1)
                    {
                        cate2_id = cate1_id;
                    }
                    else
                    {
                        cate2_id = Convert.ToInt32(Cate2.SelectedValue.ToString());
                    }
                    if (cate3_index < 1)
                    {
                        cate3_id = cate2_id;
                    }
                    else { cate3_id = Convert.ToInt32(Cate3.SelectedValue.ToString()); }

                    string productname = txtNameProduct.Text;
                    float price = float.Parse(txtPrice.Text);
                    float disconut = float.Parse(txtDiscount.Text);

                    string EncodeMota = Server.HtmlEncode(CKEditorMota.Text);
                    string EncodeChitiet = Server.HtmlEncode(CKEditorChitiet.Text);
                    string linkbaohanh = txtLinkBaohanh.Text;
                    DateTime datetime = DateTime.Now;

                    if (cbxSize.Checked)
                    {
                        activesize = "";
                    }
                    else
                    {
                        activesize = "hidden";
                    }

                    if (cbxactivediscount.Checked)
                    {
                        activediscount = "";
                    }
                    else
                    {
                        activediscount = "hidden";
                    }
                    if (cbxactivelinkbaohanh.Checked)
                    {
                        activelinkbaohanh = "";
                    }
                    else
                    {
                        activelinkbaohanh = "hidden";
                    }
                    if (cbxspch.Checked)
                    {
                        activespch = "";
                    }
                    else
                    {
                        activespch = "hidden";
                    }

                    if (LIB.Validate.CheckFileType(fulBigImage.FileName))
                    {
                        saveimage = new LIB.Validate();
                        id = DAL.DCollector.DBCollector.Instance.CallStored("callstored", "select top 1 IDBox_ID from tbl_IDBox where tbl_IDBox.IDBox_Active=1", true).Rows[0]["IDBox_ID"].ToString();
                        filePathImageAvatar = "/Images/Image-Products/" + id + "a" + Path.GetExtension(fulBigImage.FileName) + "";
                        saveimage.ThayDoiKichThuocAnh(Server.MapPath("/Images/Image-Products/"), id + "a" + Path.GetExtension(fulBigImage.FileName), 500, fulBigImage.PostedFile.InputStream);
                        string value = "" + cate1_id + "," + cate2_id + "," + cate3_id + ",N'" + productname + "'," + vendor_id + ",N'" + filePathImageAvatar + "',N'" + productname + "',N'" + EncodeMota + "',N'" + EncodeChitiet + "'," + price + "," + disconut + ",0.1,'" + activediscount + "','" + activesize + "',N'" + linkbaohanh + "',N'" + activelinkbaohanh + "',N'" + activespch + "','" + DateTime.Now.ToString() + "'";
                        List<string> values = new List<string>();
                        values.Add(value);
                        DAL.DCollector.DBCollector.Instance.StoredProcedureInsertByRows("tbl_Product", "insertproduct", values, false);
                        int check = 0;
                        DataTable dt2 = DAL.DCollector.DBCollector.Instance.SelectSQL("Select * from tbl_Color");
                        for (int i = 0; i < dt2.Rows.Count; i++)
                        {
                            int nextid = Convert.ToInt32(DAL.DCollector.DBCollector.Instance.getNextID("tbl_ProductColor", "ProductColor_ID"));
                            
                            CheckBox checkboxcolor = (CheckBox)rpt_Color.Items[i].FindControl("chkidColor");
                            if (checkboxcolor.Checked == true)
                            {
                                check = 1;
                                int idnextproductcolor = nextid + 1;
                                filePathBigImage = "/Images/Image-Products/" + id + "" + Convert.ToInt32(dt2.Rows[i]["Color_ID"].ToString()) + "a";
                                filePathSmallIamge = "/Images/Image-Products/" + id + "" + Convert.ToInt32(dt2.Rows[i]["Color_ID"].ToString()) + "b,/Images/Image-Products/" + id + "" + Convert.ToInt32(dt2.Rows[i]["Color_ID"].ToString()) + "c,/Images/Image-Products/" + id + "" + Convert.ToInt32(dt2.Rows[i]["Color_ID"].ToString()) + "d";
                                string value1 = "" + idnextproductcolor + "," + id + "," + Convert.ToInt32(dt2.Rows[i]["Color_ID"].ToString()) + ",'" + filePathBigImage + "','" + filePathSmallIamge + "'";
                                List<string> values1 = new List<string>();
                                values1.Add(value1);
                                DAL.DCollector.DBCollector.Instance.StoredProcedureInsertByRows("tbl_ProductColor", "insertbyrows", "*", values1, false);
                                values1.Clear();
                            }
                        }
                        if (check == 0)
                        {
                            int nextid = Convert.ToInt32(DAL.DCollector.DBCollector.Instance.getNextID("tbl_ProductColor", "ProductColor_ID"));
                            int idnextproductcolor = nextid + 1;
                            filePathBigImage = "/Images/Image-Products/" + id + "" + Convert.ToInt32(dt2.Rows[0]["Color_ID"].ToString()) + "a";
                            filePathSmallIamge = "/Images/Image-Products/" + id + "" + Convert.ToInt32(dt2.Rows[0]["Color_ID"].ToString()) + "b,/Images/Image-Products/" + id + "" + Convert.ToInt32(dt2.Rows[0]["Color_ID"].ToString()) + "c,/Images/Image-Products/" + id + "" + Convert.ToInt32(dt2.Rows[0]["Color_ID"].ToString()) + "d";
                            string value1 = "" + idnextproductcolor + "," + id + "," + Convert.ToInt32(dt2.Rows[0]["Color_ID"].ToString()) + ",'" + filePathBigImage + "','" + filePathSmallIamge + "'";
                            List<string> values1 = new List<string>();
                            values1.Add(value1);
                            DAL.DCollector.DBCollector.Instance.StoredProcedureInsertByRows("tbl_ProductColor", "insertbyrows", "*", values1, false);
                            values1.Clear();
                        }
                        Response.Write("<script>alert('Thêm sản phẩm thành công ...')</script>");
                    }
                    else
                    {
                        Response.Write("<script>alert('Chỉ có thể upload file(.jpg,.png,.jpeg)')</script>");
                    }
                }
            }
        }


    }
}
